<template>
  <div class="banner-edit">
    <!-- 编辑弹窗 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      :title="title"
      :dialogLoading="dialogLoading"
      dialogWidth="500px"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose">
      <div class="dialog-body" slot="dialogBody">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="名称" prop="name">
                <el-input v-model.trim="ruleForm.name" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="链接地址" prop="action">
                <el-input v-model.trim="ruleForm.action" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="排序号" prop="sort">
                <el-input v-model.trim="ruleForm.sort" :disabled="disabled" type="number"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片" prop="avatar">
                <cvue-upload 
                  cssClass="avatar-uploader"
                  :fileParams="fileParams"
                  :fileLoading="fileLoading"
                  @success="handleAvatarSuccess"
                  :actionUrl="baseServer+'/v1/api/uploadImg'">
                  <template>
                    <img v-if="imageUrl" :src="baseServer + imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </template>
                </cvue-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </cvue-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import cvueDialog from '@/components/cvue-dialog'
import { validPhone, validIp } from '@/util/validator.js'
import cvueUpload from '@/components/cvue-upload'
import {baseServer} from '@/config/config'
export default {
  name: 'banner-edit',
  components: {
    cvueDialog,
    cvueUpload
  },
  data () {
    return {
      ruleForm: {
        name: '',
        action: '',
        sort: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' }
        ]
      },
      disabled: false,
      dialogLoading: false,
      imageUrl: '',
      fileLoading: false,
      baseServer: baseServer
    }
  },
  mounted () {
    this.getEditDetail()
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    title () {
      var title = ''
      if (this.$parent.actionType == 'add') {
        title = '新增'
      } else {
        title = '编辑'
      }
      return title
    },
    fileParams () {
      return {
        'access_token': this.token
      }
    },
    ...mapGetters(['token', 'userInfo'])
  },
  methods: {
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          var params = {
            id: this.$parent.actionType == 'add' ? '' : this.$parent.rowData.id,
            name: this.ruleForm.name,
            action: this.ruleForm.action,
            sort: this.ruleForm.sort,
            photo: this.imageUrl,
            user_id: this.userInfo.id
          }
          // console.log(params)
          this.dialogLoading = true
          var url = this.$parent.actionType == 'add' ? 'shop/AdPost' : 'shop/AdPut'
          this.$store.dispatch(url, params).then(res => {
            // console.log(res)
            if (res.code === 200) {
              this.$message({
                message: res.msg,
                duration: 2000,
                type: 'success'
              })
              this.$emit('confirmDialog')
            }
            this.dialogLoading = false
          }).catch(err => {
            this.dialogLoading = false
            this.$message({message: err.msg, duration: 2000, type: 'error'})
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getDetail () {
    },
    getEditDetail () {
      if (this.$parent.actionType == 'edit') {
        this.$set(this.ruleForm, 'name', this.$parent.rowData.name)
        this.$set(this.ruleForm, 'action', this.$parent.rowData.action)
        this.$set(this.ruleForm, 'sort', this.$parent.rowData.sort)
        this.imageUrl = this.$parent.rowData.photo
      }
    },
    handleAvatarSuccess ({response, file, fileList}) {
      // console.log(response)
      this.imageUrl = response.data.filePath
    }
  }
}
</script>
<style lang="scss">
.banner-edit{
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 130px;
    height: 130px;
    line-height: 130px;
    text-align: center;
  }
  .avatar {
    width: 130px;
    height: 130px;
    display: block;
  }
}
</style>
